package practice.practice_0911;

public class StringTest {//记录一个子串在整串中出现的次数

    public static void main(String[] args) {
        String str="ABCabcqoejkacbkoabc123abcla";
        String key="abc";
        StringTest stringTest=new StringTest();
        int count=stringTest.getKeyStringCount(str,key);
        System.out.println(count);
    }
    public int getKeyStringCount(String str,String key){
        int count =0;
        //如果整串中不包含子串直接返回0
        if(str.contains(key)==false){
            return count;
        }
        //记录出现子串的索引位置
        int index=0;
        while((index=str.indexOf(key))!=-1){
            //substring将整串剩余部分截取出来
            str=str.substring(index+key.length());
            count++;
        }
        return count;
    }
}
